import http from "@/api";
import { ReqPage, ResPage } from "@/api/interface";

const baseUrl: string = "/dict";

// ==========================枚举字典==========================

export interface DictItemResult {
  value: any;
  label: string;
  extendJson: { [key: string]: any };
}

export const getEnumDictItem = (name: string) => {
  return http.get<DictItemResult[]>(baseUrl + `/enum/item`, { name: name });
};

export interface EnumListParams {
  name?: string;
  standard: boolean;
  valueType: string;
}

export interface EnumListResults {
  name: string;
  description: string;
  standard: boolean;
  valueType: string;
}

export const getEnumList = (params: EnumListParams) => {
  return http.get<EnumListResults[]>(baseUrl + `/enum/list`, params);
};

// ==========================枚举字典==========================
// ==========================字典管理==========================

export interface DictPageParam extends ReqPage {
  dictName: string;
  dictType: string;
}

export interface DictPageResult {
  id: string;
  dictName: string;
  dictType: string;
  dictStatus: number;
  remark: string;
  updateTime: string;
}

export const getDictPage = (params: DictPageParam) => {
  return http.get<ResPage<DictPageResult>>(baseUrl + `/dict/page`, params);
};

export interface DictInfoResult {
  id: string;
  dictName: string;
  dictType: string;
  dictStatus: number;
  remark: string;
}

export const getDictInfo = (id: string) => {
  return http.get<DictInfoResult>(baseUrl + `/dict/info/` + id);
};

export interface DictSaveParam {
  dictName?: string;
  dictType?: string;
  dictStatus: number;
  remark?: string;
}

export const addDict = (param: DictSaveParam) => {
  return http.post(baseUrl + `/dict/save`, param);
};

export interface DictUpdateParam {
  id: string;
  dictName?: string;
  dictType?: string;
  dictStatus: number;
  remark?: string;
}

export const updateDict = (param: DictUpdateParam) => {
  return http.post(baseUrl + `/dict/update`, param);
};

export const deleteDict = (params: string[]) => {
  return http.post(baseUrl + `/dict/delete`, params);
};

/**
 * 根据字典类型获取字典项
 * @param dictType 字典类型
 */
export const getDictItem = (dictType: string) => {
  return http.get<DictItemResult[]>(baseUrl + `/dict/item`, { dictType: dictType });
};

// ==========================字典管理==========================

// ==========================字典项管理==========================
export interface DictItemPageParam extends ReqPage {
  // 字典id是必传的
  dictId: string | string[];
  label?: string;
  value?: string;
}

export interface DictItemPageResult {
  id: string;
  dictId: string | string[];
  label: string;
  value: string;
  valueType: number;
  extendJson: string;
  sort: number;
}

export const getDictItemPage = (params: DictItemPageParam) => {
  return http.get<ResPage<DictItemPageResult>>(baseUrl + `/dict_item/page`, params);
};

export interface DictItemInfoResult {
  id: string;
  dictId: string;
  label: string;
  value: string;
  valueType: number;
  extendJson: string;
  sort: number;
}

export const getDictItemInfo = (id: string) => {
  return http.get<DictItemInfoResult>(baseUrl + `/dict_item/info/` + id);
};

export interface DictItemSaveParam {
  dictId: string | string[];
  label?: string;
  value?: string;
  valueType?: number;
  extendJson?: string;
  sort?: number;
}

export const addDictItem = (param: DictItemSaveParam) => {
  return http.post(baseUrl + `/dict_item/save`, param);
};

export interface DictItemUpdateParam {
  id: string;
  dictId: string | string[];
  label?: string;
  value?: string;
  valueType?: number;
  extendJson?: string;
  sort?: number;
}

export const updateDictItem = (param: DictItemUpdateParam) => {
  return http.post(baseUrl + `/dict_item/update`, param);
};

export const deleteDictItem = (params: string[]) => {
  return http.post(baseUrl + `/dict_item/delete`, params);
};
// ==========================字典项管理==========================
